/******************************************************************************
 * Copyright (C) 2017 Chengdu Dlhd information industry co. LTD.
 * All Rights Reserved.
 * 本软件为成都顶联互动信息产业有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package net.dgg.dggcloud.service.iboss.service.common;

import net.dgg.dggcloud.service.common.base.SessionManager;
import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.iboss.constant.ScOperationConstant;
import net.dgg.dggcloud.service.iboss.entity.ScProFlowOperating;
import net.dgg.dggcloud.service.iboss.entity.ScProOrder;
import net.dgg.dggcloud.service.iboss.entity.ScProOrderTimeState;
import net.dgg.dggcloud.service.iboss.service.sc.AllProOrderService;
import net.dgg.dggcloud.service.iboss.service.sc.ScProOrderTimeStateService;
import net.dgg.dggcloud.service.jrcrm.service.cms.CustomerRecordService;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.Map;

@Service
public class RecoverDealService extends BaseService {
    @Autowired
    private ScProOrderTimeStateService scProOrderTimeStateService;
    @Autowired
    private CustomerRecordService customerRecordService;
    @Autowired
    protected AllProOrderService allProOrderService;

    public void saveRecoverDealForSuspend(ScProOrder scProOrder, ScProFlowOperating scProFlowOperating, Date suspendTime, Map recordMap) {//暂缓的恢复办理
        scProFlowOperating.setId(this.keyWorker.nextId());
        scProOrder.setFlowLatestOperationId(scProFlowOperating.getId());
        repository.saveOrUpdate(scProOrder);
        repository.save(scProFlowOperating);
        if (null != suspendTime) {//恢复办理，计算时间
            scProOrderTimeStateService.update_recoverScProOrderTime(scProOrder.getId(), suspendTime, new Date());
            customerRecordService.saveCustomerRecord(recordMap);//写入crm
        } else {
            throw new IllegalArgumentException("未查到当前订单的暂缓时间，请核对信息！");
        }
    }

    public void saveRecoverDealForReturn(ScProOrder scProOrder, ScProFlowOperating scProFlowOperating, Map recordMap) {//退单的恢复办理
        scProFlowOperating.setId(this.keyWorker.nextId());
        scProOrder.setFlowLatestOperationId(scProFlowOperating.getId());
        repository.saveOrUpdate(scProOrder);
        repository.save(scProFlowOperating);
        customerRecordService.saveCustomerRecord(recordMap);//写入crm
    }

    public void saveRecoverDealForAbandon(ScProOrder scProOrder, ScProFlowOperating scProFlowOperating, Map recordMap) {//废单的恢复办理
        scProFlowOperating.setId(this.keyWorker.nextId());
        scProOrder.setFlowLatestOperationId(scProFlowOperating.getId());
        repository.saveOrUpdate(scProOrder);
        repository.save(scProFlowOperating);
        ScProOrderTimeState state = repository.get(ScProOrderTimeState.class, scProOrder.getId());
        if (state == null) {//如果为空，说明是从待接收废单被驳回至恢复办理
            scProOrder.setOrderTime(new Date());
            Map map = scProOrderTimeStateService.addScProOrderTimeState(scProOrder);
            try {
                this.allProOrderService.saveUpdateNodeInfo(map, SessionManager.getCurrentUser());
            } catch (Exception e) {
                String msg = e.getMessage();
                Assert.isTrue(false, StringUtils.isEmpty(msg) ? "系统出现异常！" : msg);
            }
        }
        customerRecordService.saveCustomerRecord(recordMap);//写入crm
    }

    public ScProFlowOperating queryScProFlowOperatingById(Long id) {//查最新暂缓操作记录
        ScProFlowOperating operating = (ScProFlowOperating) this.repository.getSession().createCriteria(ScProFlowOperating.class)
                .add(Restrictions.eq("proProductId", id))
                .add(Restrictions.eq("operationType", ScOperationConstant.OPERATION_ORDER_SUSPEND)).addOrder(Order.desc("createTime"))
                .setMaxResults(1).setFirstResult(0).uniqueResult();
        return operating;
    }
}
